//
// CppFunCallbackStream.cpp for CppFunCallbackStream in /home/mambou_j//babel-2014
// 
// Made by jacquesquentin mamboumouafo
// Login   <mambou_j@epitech.net>
// 
// Started on  Tue May 15 00:27:03 2012 jacquesquentin mamboumouafo
// Last update Tue May 15 00:57:58 2012 jacquesquentin mamboumouafo
//


namespace	portaudio
{

	namespace	impl
	{
	  int	cppCallbackToPaCallbackAdapter(const void *inputBuffer, void *outputBuffer, unsigned long numFrames, const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags statusFlags, void *userData);
	  {
	    FunCallbackStream::CppToCCallbackData *data = static_cast<FunCallbackStream::CppToCCallbackData *>(userData);
	    return data->funPtr(inputBuffer, outputBuffer, numFrames, timeInfo, statusFlags, data->userData);
	  }
	}

  FunCallbackStream::CppToCCallbackData::CppToCCallbackData()
  {
  }

  FunCallbackStream::CppToCCallbackData::CppToCCallbackData(CallbackFunPtr funPtr, void *userData) : funPtr(funPtr), userData(userData)
  {
  }

  void	FunCallbackStream::CppToCCallbackData::init(CallbackFunPtr funPtr, void *userData)
  {
    this->funPtr = funPtr;
    this->userData = userData;
  }

  FunCallbackStream::FunCallbackStream()
  {
  }

  FunCallbackStream::FunCallbackStream(const StreamParameters &parameters, CallbackFunPtr funPtr, void *userData) : adapterData_(funPtr, userData)
  {
    open(parameters);
  }

  FunCallbackStream::~FunCallbackStream()
  {
	try
	  {
	    close();
	  }

	catch (...)
	  {
	  }
  }

  void	FunCallbackStream::open(const StreamParameters &parameters, CallbackFunPtr funPtr, void *userData)
  {
    PaError	err = Pa_OpenStream(&stream_, parameters.inputParameters().paStreamParameters(), parameters.outputParameters().paStreamParameters(), parameters.sampleRate(), parameters.framesPerBuffer(), parameters.flags(), &impl::cppcallbackAdapter, static_cast<void *>(&adapterData_));

    if (err != paNoError)
      {
	throw PaException(err);
      }
  }
}
